{{template "admin/public/head.html" .}}
<section>
	<aside>
		{{template "admin/public/nav.html" .}}
	</aside>
	<style type="text/css">
		.mask .mask-content{height: 17vh}
		.img-1{height: 80px !important;width: 80px!important;}
		.gray{background: rgba(235,235,235,0.5);}
		#excel{position: absolute;top: 10px;right: 0;opacity: 0;width:174px;height: 40px;}
	</style>
	<article>
		<div class="top-title">
			<dl>
				<dt><h1>{{.title}}</h1></dt>
				<dd><form method="get"><input type="text" class="input" placeholder="请输入您需要搜索的产品名称！" name="search"><input type="submit" value="搜索" class="button" /></form></dd>
			</dl>
		</div>
		<div class="nav1">
			<ul>
				<a href="{{$.urlc}}"><li {{if eq .cid ""}}class="no"{{end}}>全部栏目</li></a>
				{{range .column1}}
				<a href="{{$.urlc}}cid={{.id}}&censoring={{$.censoring}}&recommend={{$.recommend}}&is_show={{$.is_show}}&search={{$.search}}"><li {{if eq $.cid .id}}class="no"{{end}}>{{.name}}</li></a>
				{{end}}
			</ul>
		</div>
		{{if eq $.admin_gid "0"}}
		<div class="nav1">
			<ul>
				<a href="{{$.urlc}}cid={{.id}}&censoring=1&recommend={{$.recommend}}&is_show={{$.is_show}}&search={{$.search}}"><li {{if eq .censoring "1"}}class="no"{{end}}>上架</li></a>
				<a href="{{$.urlc}}cid={{.id}}&censoring=0&recommend={{$.recommend}}&is_show={{$.is_show}}&search={{$.search}}"><li {{if eq .censoring "0"}}class="no"{{end}}>下架</li></a>
				<a href="{{$.urlc}}cid={{.id}}&censoring={{$.censoring}}&recommend=1&is_show={{$.is_show}}&search={{$.search}}"><li {{if eq .recommend "1"}}class="no"{{end}}>推荐</li></a>
				<a href="{{$.urlc}}cid={{.id}}&censoring={{$.censoring}}&recommend=0&is_show={{$.is_show}}&search={{$.search}}"><li {{if eq .recommend "0"}}class="no"{{end}}>不推荐</li></a>
				<a href="{{$.urlc}}cid={{.id}}&censoring={{$.censoring}}&recommend={{$.recommend}}&is_show=1&search={{$.search}}"><li {{if eq .is_show "1"}}class="no"{{end}}>已显示</li></a>
				<a href="{{$.urlc}}cid={{.id}}&censoring={{$.censoring}}&recommend={{$.recommend}}&is_show=0&search={{$.search}}"><li {{if eq .is_show "0"}}class="no"{{end}}>已隐藏</li></a>
			</ul>
		</div>
		{{end}}
		<div class="content">
			<div class="release">
				<dl>
					<dt><i class="iconfont">&#xe649;</i> <b>{{.title}}说明</b><br>
						下方是{{.title}}列表,可以进行添加,修改,删除<br>
						<p> <mark>未审核</mark>的产品将不会在小程序中显示,先按是否推荐对产品排序,然后排序数字越大产品位置越靠前</p>
					</dt>
					<dd><a href="/admin/product/add"><button type="button" class="button">添加产品</button></a></dd>
					{{if ne $.admin_gid "0"}}
					<dd style="position: relative;">
						<button type="button" class="button">Excel导入产品</button>
						<input type="file" accept=".xlsx" onchange='xlsxUpload(this)' id="excel">
						<button type="button" id="merge" class="button">产品一键同步</button>
					</dd>
					{{else}}
					<dd><a class="excelUp" href="/admin/products/all_export" target="_blank"><button type="button" class="button">全部产品信息导出</button></a></dd>
					{{end}}
				</dl>
			</div>

			<table class="table">
				<thead><th width="15%">图片</th><th width="30%">产品详情</th><th width="25%"></th><th width="15%">权限</th><th width="15%">操作</th></thead>
				{{range $v := .info}}
				<tr id="{{.id}}">
					<td class="center"><img src="{{.pathname}}" class="img-1"></td>
					<td>
						产品名称：{{.title}}（<!-- <img src="../static/all/ensign-1.png" class="ensign"> --> ID：{{.id}} ）<br>
						<b>关键词：</b><mark>{{.keywords}}</mark><br>
						主平台栏目：
						<select class="sel_{{.id}}" name="cid1" hidden>
							{{range $.column1}}
								<option value="{{.id}}" key="{{$v.id}}" {{if eq $v.cid .id}}selected{{end}}>{{.name}}</option>
							{{end}}
						</select>
						<select class="sel_{{.id}}" name="cid2" hidden>
							{{if $.column2}}
							{{range $.column2}}
								<option value="{{.id}}" key="{{$v.id}}" {{if eq $v.cid .id}}selected{{end}}>{{.name}}</option>
							{{end}}
							{{end}}
						</select>
						<select class="sel_{{.id}}" name="cid3" hidden>
							{{if $.column3}}
							{{range $.column3}}
								<option value="{{.id}}" key="{{$v.id}}" {{if eq $v.cid .id}}selected{{end}}>{{.name}}</option>
							{{end}}
							{{end}}
						</select>
						<a class="columnchange" data-id="{{.id}}" data-status="cid">{{.cname}}</a>
						{{if .gcname}}店铺栏目：{{.gcname}}{{end}}
					</td>
					<td>
						阅读数：{{.click}}<br>
						排序：{{.sort}}<br>
						添加时间：{{.addtime}}<br>
						修改时间：{{.modifytime}}
					</td>
					<td class="operation">
						<a class="change" data-id="{{.id}}" data-status="censoring">{{if eq .censoring "1"}}上架{{else}}下架{{end}}</a><br>
						<a class="change" data-id="{{.id}}" data-status="recommend">{{if eq .recommend "1"}}已推荐{{else}}未推荐{{end}}</a><br>
						<a class="change" data-id="{{.id}}" data-status="is_show">{{if eq .is_show "1"}}已显示{{else}}已隐藏{{end}}</a>
					</td>
					<td class="operation">
						<a href="/admin/product/evaluation?id={{.id}}">产品评价</a><br>
						<a href="/admin/product/edit?id={{.id}}">编辑</a><br>
						<a href="/admin/product/content?id={{.id}}">详情</a><br>
						<a class="del" data-id="{{.id}}">删除</a><br>
						{{if eq $.admin_gid "0"}}
						<a class="copy" data-id="{{.id}}">复制产品</a><br>
						{{end}}	
						<a href="javascript: void(0);" data-id="{{.id}}" class="export-imgs">导出产品图片</a><br>
						<a href="javascript: void(0);" data-id="{{.id}}" class="qrCode">生成二维码</a>
					</td>
				</tr>
				{{end}}
				{{if ne .paginator.totalpages 1}}
					<tr>
						<td  colspan="5">
							<div class="paging">
								{{if ne .paginator.currpage 1}}
								<a href="{{$.url}}page={{.paginator.firstpage}}">首页</a>
								<a href="{{$.url}}page={{.paginator.prevpage}}">&lt;&lt;</a>
								{{else}}
								<a class="disable">首页</a>
								<a class="disable">&lt;&lt;</a>
								{{end}}
								{{range $index,$page := .paginator.pages}}
								{{if eq $.paginator.currpage $page}} 
								<a class="no">{{$page}}</a>
								{{else}}
								<a href="{{$.url}}page={{$page}}">{{$page}}</a>
								{{end}}
								{{end}}
								{{if eq .paginator.currpage .paginator.totalpages}}
								<a class="disable">&gt;&gt;</a>
								<a class="disable">尾页</a>
								{{else}}
								<a href="{{$.url}}page={{.paginator.nextpage}}">&gt;&gt;</a>
								<a href="{{$.url}}page={{.paginator.totalpages}}">尾页</a>
								{{end}}
							</div>
						</td>
					</tr>
				{{end}}
			</table>


		</div>
<div class="mask">
		<div class="mask-content">
			<form id="parameter">
				<table class="table1">
					<thead><th width="10%"></th><th width="15%"></th><th width="10%"></th><th width="15%"></th><th width="10%"></th><th width="15%"></th><th width="10%"></th><th width="15%"></th></thead>
					<tr>
						<td class="genre" colspan="2">店铺id</td>
						<td colspan="6">
							<input type="text" class="input70" placeholder="需要复制过去的店铺id" name="gid" list="gid">
							<datalist id="gid">
							</datalist>
							<button type="button" class="inputbutton" data-type="shop">搜索店铺</button>
						</td>
					</tr>
				</table>
				<input type="hidden" name="id">
			</form>
			<p id="tip"></p>
			<div class="mask-form">
				<dl>
					<dt>
						<button id="qr">保存</button>
						<button id="cancle">取消</button>
					</dt>
					<dd>
					</dd>
				</dl>
			</div>
		</div>
	</div>
	</article>
</section>
<footer><!-- 系统监控内容 --></footer>
<script src="https://tcsc.oss-cn-hangzhou.aliyuncs.com/static/admin/js/jquery.jedate.min.js"></script>
<script src="https://tcsc.oss-cn-hangzhou.aliyuncs.com/static/admin/js/toast.js"></script>
<script src="https://tcsc.oss-cn-hangzhou.aliyuncs.com/static/admin/js/uppic.js"></script>
<script src="https://tcsc.oss-cn-hangzhou.aliyuncs.com/static/admin/js/xcConfirm.js"></script>
<script src="https://tcsc.oss-cn-hangzhou.aliyuncs.com/static/admin/js/app.js"></script>
<script>
	window.onload = function(){ 
	　　$(".excelUp").attr("href",`/admin/products/all_export${location.search}`)
	} 
	var column2={{.column2}};
		$(`select[name="cid1"]`).change(function(){
			var key=$(this).context.className.split("_")[1]
			var id=$(this).val()
			var name=$(this).attr("name")
			var that=$(this)
			var type
			if (name=="cid1"){
				type=0
			}else{
				type=1
			}
			$.ajax({
				url:'/admin/column/get',
				data:{id:id,type:type},
				success:function(res){
					column2=res.column2
					var html=''
					if(res.column2){
						for (var i=0;i<res.column2.length;i++){
							html+=`<option value="`+res.column2[i].id+`" >`+res.column2[i].name+`</option>`
						}
					}
					if (name=="cid1"){
						$(`.sel_${key}[name="cid2"]`).html(html)
					}else{
						$(`.sel_${key}[name="gcid2"]`).html(html)
					}
					var html2=''
					if(res.column3){
						for (var i=0;i<res.column3.length;i++){
							html2+=`<option value="`+res.column3[i].id+`" >`+res.column3[i].name+`</option>`
						}
					}
					if (name=="cid1"){
						$(`.sel_${key}[name="cid3"]`).html(html2)
					}else{
						$(`.sel_${key}[name="gcid3"]`).html(html2)
					}
				}
			})
		})
		$(`select[name="cid2"],select[name="gcid2"]`).change(function(){
			var key=$(this).context.className.split("_")[1]
			var id=$(this).val();
			var name=$(this).attr("name");
			var that=$(this)
			var type;
			$.ajax({
				url:'/admin/column/get',
				data:{id:id,type:type},
				success:function(res){
					var html=''
					if(res.column3){
						for (var i=0;i<res.column3.length;i++){
							html+=`<option value="`+res.column3[i].id+`" >`+res.column3[i].name+`</option>`
						}
					}
					if (name=="cid2"){
						$(`.sel_${key}[name="cid3"]`).html(html)
					}else{
						$(`.sel_${key}[name="gcid3"]`).html(html)
					}
				}
			})
		})
	console.log(location)
    $('.del').click(function(){
		var id=$(this).attr('data-id');
		wxc.xcConfirm("您确定要删除这条数据吗?","confirm",{onOk:function(){
			$.ajax({
				url:'/admin/del',
				data:{table:'mt_product',id:id},
				type:'get',
				success:function(res) {
					if(res==1){
						showMessage('删除成功');
						$('#'+id).css('display','none');
					}else{
						wxc.xcConfirm("操作失败,请重试","error");
					}
				}
			})	
		}});
	})
	$('.qrCode').click(function () {
		var id=$(this).attr('data-id')
		$.ajax({
			url: '/product_qrcode',
			data: {id:id},
			success: function (res) {
				if (res.status === 1) {
					location = location.origin + "/attachment?pathname=" + res.pathname
				} else {
					showMessage('操作失败,请重试')
				}
			}
		})
	})
	if ($('#merge').length) {
		$('#merge').click(function () {
			wxc.xcConfirm("您确定要同步所有产品吗?","confirm",{onOk:function(){
				$('body').toast({
			        position:'fixed',
			        animateIn:'fadeIn',
			        animateOut:'fadeOut',
			        content:'同步中...',
			        duration:'99999999',
			        isCenter:true,
			    });
			    var type = 0
			    merge()
			    type = 1
			    var timer = setInterval(merge, 1000)
			    function merge() {
			    	$.ajax({
						url: location.pathname,
						type: 'post',
						timeout : 1000,
						dataType:'json',
						data: { type: type },
						success: function (res) {
							console.log(res)
							if (res.status == 1) {
								wxc.xcConfirm("同步成功","success",{onClose:function(){
			                        location.reload()
			                    }})
							} else if (res.status == 0) {
								clearInterval(timer)
								wxc.xcConfirm("操作失败,请重试","error");
							}
						}
					})
			    }
			}});
		})
	}
	$('.copy').click(function(){
		var id=$(this).attr('data-id')
		$('#parameter').find('input[name="id"]').val(id)
		$('.mask').css('display','block')
	})
	$('#cancle').click(function(){
		$('#money').val('')
		$('.mask').css('display','none')
	})
	$('#qr').click(function(){
		if($('input[name="gid"]').val()==''){
			showMessage('请输入店铺id');
			return
		}
		var form=$('#parameter').serialize()
		$.ajax({
			url:'/admin/product/copy',
			data:form,
			success:function(res){
				if(res==1){
					$('.mask').css('display','none')
					wxc.xcConfirm("复制成功","success");
				}else{
					wxc.xcConfirm("操作失败,请重试","error");
				}
			}
		})
	})
	$('.change').click(function(){
		var id=$(this).attr('data-id');
		var status=$(this).attr('data-status');
		var that=$(this)
		// wxc.xcConfirm("您确定要删除这条数据吗?","confirm",{onOk:function(){
			$.ajax({
				url:'/admin/change',
				data:{table:'mt_product',id:id,status:status},
				type:'get',
				success:function(res) {
					if(res.status==1){
						that.html(res.info)
					}else{
						wxc.xcConfirm("操作失败,请重试","error");
					}
				}
			})	
		// }});
	})
	$('.columnchange').click(function(){
		var id=$(this).attr('data-id');
		var status=$(this).attr('data-status');
		var that=$(this)
		if ($(`select.sel_${id}`).attr("hidden")=="hidden") {
			$(`select.sel_${id}`).removeAttr("hidden")
			that.html("修改")
		} else {
			var cid = "3"
			if ($(`.sel_${id}[name="cid3"]`).val()) {
				cid = $(`.sel_${id}[name="cid3"]`).val()
			} else if ($(`.sel_${id}[name="cid2"]`).val()) {
				cid = $(`.sel_${id}[name="cid2"]`).val()
			} else {
				cid = $(`.sel_${id}[name="cid1"]`).val()
			}
			
			// wxc.xcConfirm("您确定要删除这条数据吗?","confirm",{onOk:function(){
				$.ajax({
					url:'/admin/column/change',
					data:{id:id,status:status,cid:cid},
					type:'post',
					success:function(res) {
						console.log(res)
						if(res.status==1){
							that.html(res.info)
							$(`select.sel_${id}`).attr("hidden", "true")
						}else{
							wxc.xcConfirm("操作失败,请重试","error");
						}
					}
				})	
			// }});
		}
		
	})

	function xlsxUpload(e){
		var that=$(e);
		var file=e.files[0];
		var form = new FormData();
		form.append("file",file)
		$.ajax({
		  url:"/admin/product/add_excel",
		  data:form,
		  type: "post",
		  dataType: "json",
		  cache: false,//上传文件无需缓存
		  processData: false,//用于对data参数进行序列化处理 这里必须false
		  contentType: false, //必须
		  success:function(res){
		    console.log(res)
		    if (res.status==0) {
		      window.wxc.xcConfirm("导入失败,请检查文件","error")
		    }else{
		      window.wxc.xcConfirm(res.msg,"success")
		    }
		  }
		})
	} 

	// $('.export-imgs').click(function () {
	// 	var id = $(this).attr('data-id')
	// 	$.ajax({
	// 		url: "/admin/product/export_imgs",
	// 		data: {id: id},
	// 		success: function (res) {
	// 			if (res.status == 1) {
	// 				download(res.result)
	// 			} else {
	// 				window.wxc.xcConfirm('操作失败,请重试', 'error')
	// 			}
	// 		}
	// 	})
	// })

	$('.export-imgs').click(function () {
		var id = $(this).attr('data-id')
		$.ajax({
			url: "/admin/product/zipexport",
			data: {id: id},
			success: function (res) {
				console.log(res)
				if (res.status == 1) {
					location.href = location.origin + "/" + res.pathname
				} else {
					window.wxc.xcConfirm('操作失败,请重试', 'error')
				}
			}
		})
	})

	function download(data) {
		var name = data.name, imgs = data.imgs;
		for (var i=0, len=imgs.length; i < len; i++) {
			var pathname = location.origin + imgs[i]
			var ext = imgs[i].substring(imgs[i].lastIndexOf('.')).toLowerCase()
			var fileName = name + "(图" + i + ")" +ext
			getImageBlob(pathname, reader, fileName)
		}
	}
	function getImageBlob(url, cb, fileName) {
		var xhr = new XMLHttpRequest()
		xhr.open("get", url)
		xhr.responseType = 'blob'
		xhr.onload = function () {
			if (this.status == 200) {
				cb(this.response, fileName)
			}
		}
		xhr.send()
	}
	function reader(blob, fileName) {
		var url = URL.createObjectURL(blob)
		var aDom = document.createElement('a')
		aDom.href = url
		aDom.download = fileName
		document.body.appendChild(aDom)
		aDom.click()
	}
</script>
</body>
</html>
